﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using AMPLibrary.Media;

namespace MediaLibrary.Controls
{
    /// <summary>
    /// Interaction logic for JumpList.xaml
    /// </summary>
    public partial class GoToWindow : Window
    {
        AMPLibrary.Media.MediaPlayer mediaPlayer = AMPLibrary.Media.MediaPlayer.GetMediaPlayer();                

        /// <summary>
        /// 
        /// </summary>
        public GoToWindow()
        {
            InitializeComponent();

            this.nowPlayingListView.ItemTemplateSelector = new NowPlayingDataTemplateSelector();
            
            BindContent();
        }

        /// <summary>
        /// 
        /// </summary>
        public void SetKeyboardFocus()
        {
            this.mediaSelectorTextBox.Focus();
        }

        /// <summary>
        /// 
        /// </summary>
        private void BindContent()
        {
            this.nowPlayingListView.ItemsSource = mediaPlayer.NowPlayingCollection;
            this.nowPlayingListView.Items.Refresh();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void nowPlayingListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            MediaFile mediaToPlay = (MediaFile)nowPlayingListView.SelectedItem;

            if (mediaToPlay == null)
                return;

            mediaPlayer.Play(ref mediaToPlay);

            DialogResult = true;
            this.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            this.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mediaSelectorTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                DialogResult = false;
                this.Close();
            }

            if (mediaSelectorTextBox.Text == string.Empty)
            {
                BindContent();
                return;
            }

            this.nowPlayingListView.ItemsSource = (from m in mediaPlayer.NowPlayingCollection where m.Tag.Title.ToLower().Contains(mediaSelectorTextBox.Text.ToLower()) || m.Tag.AlbumArtist.ToLower().Contains(mediaSelectorTextBox.Text.ToLower()) select m);
            this.nowPlayingListView.Items.Refresh();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                DialogResult = false;
                this.Close();
            }
        }
    }
}
